package com.yc.forum.dao;


import com.yc.forum.util.DBHelper;
import com.yc.forum.util.Utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

public class CategoryDao {
    //获取一个问题的回复帖的信息
    public List<Map<String,Object>> selectPostWithLimitOrderByTime(String problemId, int begin, int size){
        String sql="select * from post where post.problemkey=? order by begintime DESC limit ?,?";
        List<Map<String,Object>>list= DBHelper.selectListMap(sql,problemId,begin,size);
        return list;
    }
    //根据帖子id获取一条帖子的详细详细
    public Map<String,Object> selectOnePostById(String postid){
        String sql="select * from post where id=?";
        return DBHelper.selectOneMap(sql,postid);
    }


    public static void main(String[] args) {
        CategoryDao c=new CategoryDao();
        System.out.println(c.select_one_postAndUser("1").toString());;
        c.select_one_postAndUser("2");
    }


/*---------------------------------------------------------------------------------------------------------------------*/

    private static JedisPool jedisPool;
    static{
            jedisPool=new JedisPool("127.0.0.1",6379);
    }
    Jedis jedis=jedisPool.getResource();


    /**
     * 获取一条帖子的详细信息（包括发帖用户信息，点赞、评论、收藏数量，帖子信息）
     * @param postid
     * @return
     */
    public Map<String,Object> select_one_postAndUser(String postid){
        String sql="select * from post,user where post.id=? and user.account=post.account";
        Map<String,Object> map=DBHelper.selectOneMap(sql,postid);

        Map<String,Object> date=Utils.dateToYearMonthDay(""+map.get("begintime"));

        map.put("year",date.get("year"));
        map.put("month",date.get("month"));
        map.put("date",date.get("date"));

        //获取该帖子点赞、等数量
        Map<String,String> sum=jedis.hgetAll("post_"+postid);
        //存入
        map.put("commentsum",sum.get("commentsum"));
        map.put("collectsum",sum.get("collectsum"));
        map.put("likesum",sum.get("likesum"));
        return map;
    }

    /**
     *获取一名用户的所有收藏记录
     *
     * @param account 用户的账号
     * @return 该用户的所有收藏记录
     */
    public List<Map<String,Object>> select_user_collection_post(String account){//"collection_" + 账号
        //获取收藏帖子id
        List<String> collectionList=jedis.lrange("collection_"+account,0,-1);

        //通过id去mysql数据库查找
        List<Map<String,Object>> list = new ArrayList<>();
        if(collectionList!=null&&collectionList.isEmpty()==false) {

            for (String id : collectionList) {//获取到的数据不需要排序，因为在redis中是降序的
                String sql = "select * from post where id=? ";
                List<Object> params=new ArrayList<Object>();
                params.add(id);
                Map<String,Object> map=new HashMap<>();
                map=DBHelper.selectOneMap(sql,params.toArray());
                //获取帖子发布时间的年份
                String begintime=""+map.get("begintime");

                //获取年月日
                Map<String,Object> date= Utils.dateToYearMonthDay(begintime);
                map.put("year",date.get("year"));
                map.put("month",date.get("month"));
                map.put("date",date.get("date"));

                //放入list
                list.add(map);
            }
        }
        return list;
    }


}

